// pages/registration/registration.js
const DB = wx.cloud.database().collection("registration")
var util = require('../../utils/util.js')
const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
var regIdNo = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
var regPos = /^\d+$/; // 非负整数
var reZS = /^\d+(?=\.{0,1}\d+$|$)/ // 正数

let dict = {
  '1': '流鼻涕',
  '2': '咽喉痛',
  '3': '精神不佳',
  '4': '呼吸不畅',
  '5': '厌食',
  '6': '畏寒',
  '7': '胸痛',
  '8': '恶心',
  '9': '呕吐',
  '10': '腹胀',
  '11': '腹泻',
  '12': '嗜睡',
}

Page({
  data: {
    is_submit: false,
    address_id: '',
    address_name: '',
    datetime: util.formatTime(new Date(), 0),
    items: [{
        name: "1",
        value: '出'
      },
      {
        name: "2",
        value: '入'
      }
    ],
    items1: [{
        name: "1",
        value: '男'
      },

      {
        name: " 2",
        value: '女'
      },
    ],
    items2: [

      {
        name: "1",
        value: '是',
      },

      {
        name: "2",
        value: '否',
      },
    ],
    items3: [{
        name: '1',
        value: '流鼻涕'
      },
      {
        name: '2',
        value: '咽喉痛'
      },
      {
        name: '3',
        value: '精神不佳'
      },
      {
        name: '4',
        value: '呼吸不畅'
      },
      {
        name: '5',
        value: '厌食'
      },
      {
        name: '6',
        value: '畏寒'
      },
      {
        name: '7',
        value: '胸痛'
      },
      {
        name: '8',
        value: '恶心'
      },
      {
        name: '9',
        value: '呕吐'
      },
      {
        name: '10',
        value: '腹胀'
      },
      {
        name: '11',
        value: '腹泻'
      },
      {
        name: '12',
        value: '嗜睡'
      },
    ]
  },
  onLoad(options) {
    let that = this
    if (JSON.stringify(options) !== '{}' && options.scene !== undefined) {
      // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
      const scene = decodeURIComponent(options.scene)
      this.setData({
        address_id: scene
      })
      wx.cloud.callFunction({
        name: 'getAddress',
        data: {
          addressId: scene
        }
      }).then(res => {
        if (res.result) {
          that.setData({
            address_name: res.result.address_name
          })
        }
      })
    }
  },
  formSubmit: function(e) {
    var that = this
    var item = e.detail.value
    if (that.data.is_submit) {
      wx.showToast({
        title: '您已经登记过了',
        icon: 'none',
        duration: 2000
      })
    } else if (item.type && item.name && item.sex && item.age && item.tel && item.temperature && item.is_cough && item.id_card && item.origin && item.destination) {
      if (!regPos.test(item.age)) {
        wx.showToast({
          title: '年龄有误',
          icon: 'none',
          duration: 2000
        })
        return
      }
      if (!reg.test(item.tel)) {
        wx.showToast({
          title: '请输入正确的手机号',
          icon: 'none',
          duration: 2000
        })
        return
      }
      if (!regIdNo.test(item.id_card)) {
        wx.showToast({
          title: '身份证号有误',
          icon: 'none',
          duration: 2000
        })
        return
      }
      if (!reZS.test(item.temperature)) {
        wx.showToast({
          title: '体温有误',
          icon: 'none',
          duration: 2000
        })
        return
      }
      let list = []
      for (let i in item.other_symptoms) {
        list.push(dict[item.other_symptoms[i]])
      }
      DB.add({
        data: {
          address_id: that.data.address_id,
          time: new Date(this.data.datetime.replace(/-/g, "/")).getTime(),
          type: item.type,
          name: item.name,
          sex: item.sex,
          age: parseInt(item.age),
          id_card: item.id_card,
          tel: item.tel,
          temperature: parseFloat(item.temperature),
          is_cough: item.is_cough,
          other_symptoms: list,
          car_id: item.car_id,
          origin: item.origin,
          destination: item.destination,
          info_added: item.info_added
        }
      }).then(res => {
        wx.showModal({
          showCancel: false,
          content: '登记成功',
          success(res) {
            if (res.confirm) {
              that.setData({
                is_submit: true
              })
            }
          }
        })
      })
    } else {
      wx.showToast({
        title: '请填写完整的信息',
        icon: 'none',
        duration: 2000
      })
    }
  }
})